#include "TwOgreColorVariable.h"

#include "TwOgreWindow.h"
#include "TwOgreWindowManager.h"

namespace TwOgre{
	ColorVariable::ColorVariable(Window *parent, const Ogre::String& name, bool readOnly, const Ogre::String& label, 
		const Ogre::ColourValue& color,	const Ogre::String& groupName, const Ogre::String& help) :
		Variable(parent, name, TW_TYPE_COLOR4F), mColor(color)
	{
		mTweakColor[0] = mColor.r;
		mTweakColor[1] = mColor.g;
		mTweakColor[2] = mColor.b;
		mTweakColor[3] = mColor.a;

		if(readOnly)
		{
			TwAddVarRO(mParent->getTweakBar(), mName.c_str(), TW_TYPE_COLOR4F, mTweakColor, "");
		}
		else
		{
			TwAddVarRW(mParent->getTweakBar(), mName.c_str(), TW_TYPE_COLOR4F, mTweakColor, "");
		}

		if(label.compare("") != 0)
		{
			setLabel(label);
		}
		if(groupName.compare("") != 0)
		{
			setGroup(groupName);
		}
		if(help.compare("") != 0)
		{
			setHelp(help);
		}
	}

	ColorVariable::ColorVariable(ColorParams& params) :
        Variable(params.parent_, params.name_, params.type_), mColor(params.value_)
	{
		mTweakColor[0] = mColor.r;
		mTweakColor[1] = mColor.g;
		mTweakColor[2] = mColor.b;
		mTweakColor[3] = mColor.a;

        if((params.getCB_ != NULL) && (params.setCB_ != NULL) && (params.data_ != NULL))
        {
            TwAddVarCB(mParent->getTweakBar(), mName.c_str(), params.type_, params.setCB_, params.getCB_, params.data_, "");
        }
        else
        {
		    if(params.readOnly_)
		    {
                TwAddVarRO(mParent->getTweakBar(), mName.c_str(), params.type_, mTweakColor, "");
		    }
		    else
		    {
                TwAddVarRW(mParent->getTweakBar(), mName.c_str(), params.type_, mTweakColor, "");
		    }
        }
		
        if(params.label_.compare("") != 0)
		{
			setLabel(params.label_);
		}
		if(params.group_.compare("") != 0)
		{
			setGroup(params.group_);
		}
		if(params.help_.compare("") != 0)
		{
			setHelp(params.help_);
		}
    }
	
    ColorVariable::~ColorVariable()
	{
		TwRemoveVar(mParent->getTweakBar(), mName.c_str());
	}

	const Ogre::ColourValue& ColorVariable::getColorValue() const
	{
		// Get the values form the control and store them in Ogre's ColourValue
		mColor = Ogre::ColourValue(mTweakColor[0], mTweakColor[1], mTweakColor[2], mTweakColor[3]);
		return mColor;
	}

	void ColorVariable::setColorValue(const Ogre::ColourValue& color)
	{
		// Convert Ogre's ColourValue into a float[4] {RGBA}
		mColor = color;
		mTweakColor[0] = mColor.r;
		mTweakColor[1] = mColor.g;
		mTweakColor[2] = mColor.b;
		mTweakColor[3] = mColor.a;
	}
}; // End Namespace
